require 'rake/testtask'

def p_s(message)
  print "\e[35;1m#{message}\e[0m\n"
end

desc '统计信息'
task :status do
  p_s '当前状态'
end

desc '测试'
Rake::TestTask.new do |t|
  t.libs << 'test'
  t.test_files = FileList['test/test_*.rb']
  t.verbose = true
end

desc '启动服务'
task :server do |t|

end

desc '任务列表'
task :default do
  p_s 'rake -T 可以查看详细信息'
  puts Rake.application.tasks
end


namespace :server do
  PID='tmp/.pid'

  desc '启动服务'
  task :start do
    if File.exist?(PID)
      p_s "服务已经启动，如果进程不存在，请删除[#{PID}]文件"
    else
      system "rackup -s thin config.ru -P #{PID} -p 8080 -E production -D"
      p_s '操作成功'
    end
  end

  desc '停止服务'
  task :stop do
    if File.exist?(PID)
      File.open PID do |file|
        system "kill -9 #{file.read}"
      end
      FileUtils.rm PID
      p_s '操作成功'
    else
      p_s '服务未启动'
    end
  end

  desc '重启服务'
  task :restart => [:stop, :start]

  desc '调试模式'
  task :debug do
    system "rerun 'rackup -p 8080'"
  end

end


